package cn.iocoder.yudao.module.shop.controller.admin.memberpay;

import cn.iocoder.yudao.module.shop.dal.dataobject.memberpay.ShopMemberPayItemDO;
import cn.iocoder.yudao.module.shop.service.memberpay.ShopMemberPayItemService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.memberpay.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberpay.ShopMemberPayDO;
import cn.iocoder.yudao.module.shop.convert.memberpay.ShopMemberPayConvert;
import cn.iocoder.yudao.module.shop.service.memberpay.ShopMemberPayService;

@Tag(name = "管理后台 - 每支价格")
@RestController
@RequestMapping("/shop/member-pay")
@Validated
public class ShopMemberPayController {

    @Resource
    private ShopMemberPayService memberPayService;

    @Resource
    private ShopMemberPayItemService memberPayItemService;

    @PostMapping("/create")
    @Operation(summary = "创建每支价格")
    @PreAuthorize("@ss.hasPermission('shop:member-pay:create')")
    public CommonResult<Long> createMemberPay(@Valid @RequestBody ShopMemberPayCreateReqVO createReqVO) {
        return success(memberPayService.createMemberPay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新每支价格")
    @PreAuthorize("@ss.hasPermission('shop:member-pay:update')")
    public CommonResult<Boolean> updateMemberPay(@Valid @RequestBody ShopMemberPayUpdateReqVO updateReqVO) {
        memberPayService.updateMemberPay(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除每支价格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:member-pay:delete')")
    public CommonResult<Boolean> deleteMemberPay(@RequestParam("id") Long id) {
        memberPayService.deleteMemberPay(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得每支价格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:member-pay:query')")
    public CommonResult<ShopMemberPayRespVO> getMemberPay(@RequestParam("id") Long id) {
        ShopMemberPayDO memberPay = memberPayService.getMemberPay(id);
        return success(ShopMemberPayConvert.INSTANCE.convert(memberPay));
    }

    @GetMapping("/list")
    @Operation(summary = "获得每支价格列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:member-pay:query')")
    public CommonResult<List<ShopMemberPayRespVO>> getMemberPayList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopMemberPayDO> list = memberPayService.getMemberPayList(ids);
        return success(ShopMemberPayConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得每支价格分页")
    @PreAuthorize("@ss.hasPermission('shop:member-pay:query')")
    public CommonResult<PageResult<ShopMemberPayRespVO>> getMemberPayPage(@Valid ShopMemberPayPageReqVO pageVO) {
        PageResult<ShopMemberPayDO> pageResult = memberPayService.getMemberPayPage(pageVO);
        return success(ShopMemberPayConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出每支价格 Excel")
    @PreAuthorize("@ss.hasPermission('shop:member-pay:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberPayExcel(@Valid ShopMemberPayExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopMemberPayDO> list = memberPayService.getMemberPayList(exportReqVO);
        // 导出 Excel
        List<ShopMemberPayExcelVO> datas = ShopMemberPayConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "每支价格.xls", "数据", ShopMemberPayExcelVO.class, datas);
    }

    //////////////////////////////ShopMemberPayItem//////////////////////////////////////////////

    @PostMapping("/price/update")
    @Operation(summary = "设置或更新每支价格")
    @PreAuthorize("@ss.hasPermission('shop:member-pay:update')")
    public CommonResult<Boolean> createOrUpdatePrice(@Valid @RequestBody ShopMemberPayItemCreateReqVO createReqVO){
        ShopMemberPayItemDO itemDO = memberPayItemService.getMemberPayItemByMemberIdNoExp(createReqVO.getMemberId());
        if(itemDO == null){
            memberPayItemService.createMemberPayItem(createReqVO.getPriceId(), createReqVO.getMemberId());
        }else {
            memberPayItemService.updpateMemberPayItem(ShopMemberPayConvert.INSTANCE.convertUpdateReq(itemDO, createReqVO));
        }

        return success(true);
    }
}
